package top.laoshuzi.cloudplatform.servo.ui;


import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.io.IOException;

import top.laoshuzi.cloudplatform.R;
import top.laoshuzi.cloudplatform.servo.ServoHandler;

/**
 * A simple {@link Fragment} subclass.
 */
public class ServoFragment extends Fragment {

    Button up;
    Button down;
    Button left;
    Button right;
    Button reset;

    double angle_h;
    double angle_v;
    double angle_interval;

    ServoHandler servoHandler;

    public ServoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_servo, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        up = view.findViewById(R.id.up);
        down = view.findViewById(R.id.down);
        left = view.findViewById(R.id.left);
        right = view.findViewById(R.id.right);
        reset = view.findViewById(R.id.reset);
        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    angle_v += angle_interval;
                    servoHandler.setVerticalServoAngle(angle_v);
                    System.out.println(angle_v);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    angle_v -= angle_interval;
                    servoHandler.setVerticalServoAngle(angle_v);
                    System.out.println(angle_v);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    angle_h += angle_interval;
                    servoHandler.setHorizontalServoAngle(angle_h);
                    System.out.println(angle_h);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    angle_h -= angle_interval;
                    servoHandler.setHorizontalServoAngle(angle_h);
                    System.out.println(angle_h);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    angle_h = 135;
                    angle_v = 135;
                    servoHandler.setHorizontalServoAngle(angle_h);
                    servoHandler.setVerticalServoAngle(angle_v);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        servoHandler = ServoHandler.getInstance();
        try {
            servoHandler.initialize(-0.15, -0.05);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            angle_h = 135;
            angle_v = 135;
            angle_interval = 10;
            servoHandler.setHorizontalServoAngle(angle_h);
            servoHandler.setVerticalServoAngle(angle_v);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        try {
            servoHandler.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
